package Admin;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JTextArea;

import com.example.finalproj.DB_VO;

public class DB_Thread extends Thread{
	
	Socket s;
	JTextArea ta_DBLog;
	ObjectOutputStream oos;
	ObjectInputStream ois;
	String conid;
	Connection conn;
	PreparedStatement pst;
	ResultSet rs;
	String grade;
	String client_ip;
	
	public DB_Thread(Socket s, JTextArea ta_DBLog) {
		this.s = s;
		client_ip = s.getInetAddress().getHostAddress();
		this.ta_DBLog = ta_DBLog;
		start();
	}

	@Override
	public void run() {
		try
		{
			oos = new ObjectOutputStream(s.getOutputStream());
			ois = new ObjectInputStream(s.getInputStream());
			boolean flag = true;
			while(flag)
			{
				String sql;
				DB_VO dv;
				dv = (DB_VO)ois.readObject();
				switch(dv.getAction())
				{
				case 1: // 로그인 (아이디확인) & 액션컨펌 발송
					conid = (String)dv.getObj();
					if(!conid.equals(""))
					{
//						sql = "select grade from member where mid="+conid;
						sql = "select grade from member where mid=?";
						conn = new MySqlDB().getConn();
						pst = conn.prepareStatement(sql);
						pst.setString(1, conid);
						rs = pst.executeQuery();
						rs.next();
						grade = rs.getString("grade");
						if(grade.equals("3"))
						{
							ta_DBLog.append(client_ip+" - "+conid+" 접속\n");
						}
						else
						{
							ta_DBLog.append(client_ip+" - "+conid+" 디자이너 접속 Grade("+grade+")\n");
						}
					}
					else
					{
						ta_DBLog.append(client_ip+" - "+"익명사용자 접속\n");
					}
					
					dv = new DB_VO();
					
					dv.setAction(2); // 컨펌 발송
					oos.writeObject(dv);
					break;
				case 11:
					ta_DBLog.append(client_ip+" - "+"로그인 요청\n");
					String id = (String)dv.getObj();
					String pw = (String)dv.getObj2();
					sql = "select pwd from member where mid=?";
					conn = new MySqlDB().getConn();
					pst = conn.prepareStatement(sql);
					pst.setString(1, id);
					rs = pst.executeQuery();
					String idpw = "3";
					if(rs.next())
					{
						idpw="2";
						if(rs.getString("pwd").equals(pw))
						{
							idpw="1";
							ta_DBLog.append(client_ip+" - "+id+"로그인 처리\n");
						}
					}
					if(!idpw.equals("1"))
					{
						ta_DBLog.append(client_ip+" - "+id+"로그인 실패\n");
					}
					dv = new DB_VO();
					dv.setAction(11);
					dv.setObj(idpw);
					oos.writeObject(dv);
					
					break;
				case 31: // 디자이너 리스트 요청
					if(grade.equals("3")) // 일반 회원일 경우
					{
						ta_DBLog.append(conid+" 디자이너 리스트 요청\n");
						sql = "select m.mid, m.irum, d.grade from member m join designer d on m.mid=d.mid where m.grade=2";
						conn = new MySqlDB().getConn();
						pst = conn.prepareStatement(sql);
						rs = pst.executeQuery();
						
						ArrayList<String> dmid = new ArrayList<String>();
						HashMap<String, String> designer;
						ArrayList<HashMap<String, String>> dlist = new ArrayList<HashMap<String,String>>();
						
						while(rs.next())
						{
							designer = new HashMap<String, String>();
							String irum = rs.getString("m.irum");
							String mid = rs.getString("m.mid");
							String grade1 = rs.getString("d.grade");
	//						System.out.println(irum+"/"+mid+"/+grade1");
							designer.put("irum", rs.getString("m.irum"));
							String grade = grade1;
//							switch(rs.getInt("d.grade"))
//							{
//							case 1:
//								grade = "원장";
//								break;
//							case 2:
//								grade = "실장";
//								break;
//							case 3:
//								grade = "디자이너";
//								break;
//							default:
//								grade = "디자이너";
//								break;
//							}
							designer.put("grade", grade);
							dlist.add(designer);
							dmid.add(rs.getString("m.mid"));
						}
						dv = new DB_VO();
						dv.setAction(31);
						dv.setObj(dlist);
						dv.setObj2(dmid);
						oos.writeObject(dv);
						conn.close();
						conn = null;
						ta_DBLog.append(conid+" 디자이너 리스트 처리완료\n");
					}
					else // 디자이너 이상일 경우
					{
						dv = new DB_VO();
						dv.setAction(91); // action_reconnect_chat 채팅서버로 접속을 유도
						oos.writeObject(dv);
					}
					break;
				case 100: // 작업후에 종료하기 ACTION_END
					flag=false;
					break;
				}
			}
			ois.close();
			oos.close();
			s.close();
			s = null;
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
	}
	
	
	
}
